package dman;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Class contain DB records and methods to work with them
 */
public class VIN_DB {

    public ArrayList VINs;
    public File[] listOfFiles;         // list of files in DOCS

    public class VINrecord {
        public int ID;
        public String VIN;
        public String Desc;
        public String FileName;
    }

    VIN_DB(String DOCS_FOLDER, String DB_FILE)  {
        VINs=new ArrayList();
        LoadFileList (DOCS_FOLDER, DB_FILE);
    }

    public void LoadFileList (String DOCS_FOLDER, String DB_FILE) {
// Read list of files in doc folder. Save list in "VINs"
        VINs.clear();
        File folder = new File(DOCS_FOLDER);
        listOfFiles = folder.listFiles();
        if (listOfFiles!=null) {
            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                   VINrecord record = new VINrecord();
                   record.FileName = listOfFiles[i].getName();
                   record.VIN="";
                   record.Desc="";
                   record.ID=0;
                   VINs.add(record);
                   }
            }
    // Read DB with description and VINs of files
            CSVReader reader;
            try {
                reader = new CSVReader(new FileReader(DB_FILE));
                String [] nextLine;
                try {
                    while ((nextLine = reader.readNext()) != null) {
                        if (!nextLine[0].equalsIgnoreCase("ID")) {
                            int j = UpdateRecordByFileName (
                                Integer.parseInt(nextLine[0]),
                                nextLine[1].trim(),
                                nextLine[2].trim(),
                                nextLine[3].trim());
                            if (j<0) {
                        /// file for record is deleted - process it -----------------------
                            }
                        }
                    }
                    reader.close();
                } catch (IOException ex) {
                     System.out.println ("Error: "+ex.getMessage());
                }
            }
            catch (FileNotFoundException ex) {
                System.out.println ("Error: "+ex.getMessage());
            }
        }
    }

    public void replaceVIN (String old, String nova) {
        Iterator iterator = VINs.iterator();
        while (iterator.hasNext()) {
            VINrecord record = (VINrecord) iterator.next();
            if (record.VIN.equalsIgnoreCase(old))
                record.VIN=nova;
        }
    }

    public String FileByVINandDesc(String VIN, String Desc) {
//        String VIN=list1.getSelectedItem();
//        String Desc=list2.getSelectedItem();
        String Res = "";
        Iterator iterator = VINs.iterator();
        while (iterator.hasNext()) {
            VINrecord record = (VINrecord) iterator.next();
            if (VIN.equalsIgnoreCase(record.VIN) & Desc.equalsIgnoreCase(record.Desc))
                   Res=record.FileName;
        }
        return Res;
    }

    public VINrecord getRecordByFileName(String FName) {
        VINrecord res = null;
        VINrecord record;
        for (int i=0; i<VINs.size(); i++){
          record=(VINrecord)VINs.get(i);
          //LogText ("comparing: "+FileName+" to: "+record.FileName);
          if (FName.equalsIgnoreCase(record.FileName)) {
             res=record;
             //LogText ("found: "+FName);
             break;
          }
        }
        return res;
    }

    public int getNewID() {
        int j=1;
        VINrecord record;
        for (int i=0; i<VINs.size(); i++){
          record=(VINrecord)VINs.get(i);
          if (j<=record.ID)
             j=record.ID+1;
        }
        return j;
    }

    public int UpdateRecordByFileName (int ID, String VIN, String Desc, String FileName){
       int res=-1;
       VINrecord record;
       for (int i=0; i<VINs.size(); i++){
          record=(VINrecord)VINs.get(i);
          //LogText ("comparing: "+FileName+" to: "+record.FileName);
          if (FileName.equalsIgnoreCase(record.FileName)) {
             record.Desc=Desc;
             record.VIN=VIN;
             record.ID=ID;
             res=i;
             //LogText ("found: "+FileName);
             break;
          }
        }
       return res;
    }

    public void SaveDB (String DB_FILE) {
        try {
            CSVWriter writer = new CSVWriter(new FileWriter(DB_FILE));
            VINrecord record;
            for (int i = 0; i < VINs.size(); i++) {
                record = (VINrecord) VINs.get(i);
                if (record.ID>0) {
                    String[] entries = new String [4];
                    entries[0]=Integer.toString(record.ID);
                    entries[1]=record.VIN;
                    entries[2]=record.Desc;
                    entries[3]=record.FileName;
                    writer.writeNext(entries);
                }
            }
            writer.close();
        } catch (IOException ex) {
            System.out.println("Error: "+ex.getMessage());
        }
    }

}
